<?php

/**
 * home actions.
 *
 * @package    Redsea20
 * @subpackage home
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class homeActions extends sfActions
{   
    public function executeSendMessage($request)
    {
        $params = $request->getParameter('message');
        
        $toEntity = Entity::GetEntityByEmail(substr($params['email'], strrpos($params['email'], '(') + 1, -1) );

        Message::createNewMessage($this->getUser()->getAttribute("currentuser")->getId(), $toEntity->getId(), $params['subject'], $params['body']);
        $this->getUser()->setFlash('msg', 'Message has been sent successfully!.');

        // Redirecting it to same page so that form will not be submitted again on F5
        $this->redirect("home/messages");

        //$temp = http_build_query($request->getParameter('message'));         
        return sfView::NONE;
    }

    public function executeFriends(sfWebRequest $request)
    {
        if ($this->getRequestParameter('flag') != null && $this->getRequestParameter('flag') != "")
        {


        }
        return sfView::SUCCESS;
    }

    /**
     *
     * @param sfWebRequest $request
     */
    public function executeMessages(sfWebRequest $request)
    {
        //RBISUtility::SendEmail('ali.dars@kaust.edu.sa', 'adars@saudioger.com', 'Testing Email (With Passed Mailer)-4', 'Testing RBIS Email Service');
        $this->form = new MessageForm();
        return sfView::SUCCESS;
    }

    /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    //$this->forward('default', 'module');

        if($this->getUser()->isAuthenticated())
        {
            //Entity::doSelect
        }
        
        return sfView::SUCCESS;
  }

  public function executeConnectionAutocomplete(sfWebRequest $request)
  {
      $currentUser = $this->getUser()->getAttribute("currentuser");

      $connInfo = '';
      $this->connectionDetail = $currentUser->GetConnectionListWithEmail();
      foreach($this->connectionDetail as $connDetail)
          $connInfo = $connInfo.$connDetail["firstname"]." ".$connDetail["lastname"]." (".$connDetail["emailaddress"].")"."###";

      echo $connInfo;
      return sfView::NONE;
  }

  public function executeShowMessage(sfWebRequest $request)
  {
      
//    $messageContent = "";
    if ($this->getRequestParameter('id') != null && $this->getRequestParameter('id') != "")
    {
        $this->message = Message::GetMessageContent($this->getRequestParameter('id'), $this->getUser()->getAttribute("currentuser")->getId());
        $this->messageSender = Entity::GetEntity($this->message->getFromentityid());
//
//    // Check the string if it already contains separator (i-e ###) then get it replaced with *** and at client side replace back to ###
//    echo $message->getId()."###".Entity::GetEntity($message->getFromentityid())->getFirstname()."###".str_replace("###", "***", $message->getSubject())."###".str_replace("###","***",$message->getBody());
    }

      return sfView::SUCCESS;
  }

  public function executeDeleteMessage(sfWebRequest $request)
  {

    if ($this->getRequestParameter('id') != null && $this->getRequestParameter('id') != "")
    {
      $deleteStatus = Message::DeleteUserMessage($this->getRequestParameter('id'));

      echo $deleteStatus;
    }

    return sfView::NONE;
  }

  public function executeSendFriendRequest(sfWebRequest $request)
  {    
    if ($this->getRequestParameter('txtRequestedIds') != null && $this->getRequestParameter('txtRequestedIds') != "")
    {
        $emailList = explode(",", $this->getRequestParameter('txtRequestedIds'));
        
        foreach ($emailList as $email) {
            RBISUtility::SendEmail($email, "ali.dars@kaust.edu.sa", "[RBIS]Friend Request", "Dear Member, <BR/> You have requested by Fahmi, to be member of RBIS community. Please click the given below link to create an account at RBIS.<BR/> Thanks");
        }
        
    }
    return sfView::NONE;
  }

  public function executeSendMessageReply(sfWebRequest $request)
  {
      //echo $this->getUser()->getAttribute("userid");
      MessageReply::createReply($this->getUser()->getAttribute("currentuser")->getId(), $request->getParameter('hfMsgID'), $request->getParameter('Reply'));

      // Redirecting it to same page so that form will not be submitted again on F5
      $this->redirect("home/messages");
      
      return sfView::NONE;

  }

  public function executeGetMessageReply(sfWebRequest $request)
  {
    if ($this->getRequestParameter('mid') != null && $this->getRequestParameter('mid') != "")
    {
        
        $currentMessage = Message::GetMessage($this->getRequestParameter('mid'));

        echo "<table>";

        foreach($currentMessage->getMessageReply() as $messageReply){
            $entity = Entity::GetEntity($messageReply->getFromEntityId());
            echo "<tr>";
            echo "<td rowspan=\"2\" width=\"50px\" valign=\"top\"/>";
            echo "<td><b>";
            echo $entity->getFirstname();
            echo "</b></td>";

            echo "<td>";
            echo $messageReply->getReplybody();
            echo "</td>";

            echo "</tr>";
        }
        echo "</table>";
    }
    

    return sfView::NONE;
  }

  public function getFriendList(bit $loadAll)
  {
      $result = Doctrine_Query::create()
                        ->select('COUNT(ec.id)')
                        ->from('entityconnection ec')
                        ->leftJoin('ec.Entity e')
                        ->where('e.id='.$this->getUser()->getAttribute('currentuser')->getId())
                        ->andwhere('ec.connectionstatustyperefid='.Types::$CONN_STATUS["PENDING"]);
  }
}
